package com.nabicht.bigblankpage.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.Document;

public class BBPPanel extends JPanel {

	private final static int sidePanelWidth = 50;
	private final static int minHeight = 300;
	
	private final int editorWidth;
	
	//GUI components
	private final JTextPane textPane = new JTextPane();
	private final JScrollPane textScrollPane = new JScrollPane(textPane);
	private JPanel leftPanel = new JPanel();
	private JPanel rightPanel = new JPanel();
	
	
	public BBPPanel() {

		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		editorWidth = Math.min(600, screenSize.width - (sidePanelWidth * 2) - 100); 
		
		//Dimensions
		Dimension sidePanelMinDim = new Dimension(sidePanelWidth, minHeight);
		Dimension sidePanelPreferredDim = new Dimension(sidePanelWidth, minHeight);
		Dimension sidePanelMaxDim = new Dimension((screenSize.width - editorWidth)/2, screenSize.height);
		
		Dimension scrollPaneMinDim = new Dimension(sidePanelWidth, minHeight);
		Dimension scrollPanePreferredDim = new Dimension(editorWidth, minHeight);
		Dimension scrollPaneMaxDim = new Dimension(editorWidth, screenSize.height);
		
		Dimension topBottomPanelDim = new Dimension(editorWidth + (2 * sidePanelWidth), 25);
		
		leftPanel.setPreferredSize(sidePanelPreferredDim);
		leftPanel.setMinimumSize(sidePanelMinDim);
		leftPanel.setMaximumSize(sidePanelMaxDim);
		
		rightPanel.setPreferredSize(sidePanelPreferredDim);
		rightPanel.setMinimumSize(sidePanelMinDim);
		rightPanel.setMaximumSize(sidePanelMaxDim);
		
		textScrollPane.setPreferredSize(scrollPanePreferredDim);
		textScrollPane.setMinimumSize(scrollPaneMinDim);
		textScrollPane.setMaximumSize(scrollPaneMaxDim);
		//turn off scrollpane's border
		textScrollPane.setBorder(null);
		//TODO come up with a way of doing scrolling without needing scrollbars
		
		setBackgroundColor(Color.BLACK);  //TODO I'm going to have to get this from a config
		
		setFontColor(Color.GREEN);	//TODO I'm going to have to get this from a config
		setCaretColor(Color.GREEN);	//TODO I'm going to have to get this from a config
		
		JPanel topPanel = new JPanel();
		topPanel.setPreferredSize(topBottomPanelDim);
		topPanel.setMinimumSize(topBottomPanelDim);
		topPanel.setMaximumSize(topBottomPanelDim);
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.setPreferredSize(topBottomPanelDim);
		bottomPanel.setMinimumSize(topBottomPanelDim);
		bottomPanel.setMaximumSize(topBottomPanelDim);
		
		JPanel horizontalPanel = new JPanel();
		horizontalPanel.setLayout(new BoxLayout(horizontalPanel, BoxLayout.X_AXIS));
		horizontalPanel.add(leftPanel);
		horizontalPanel.add(textScrollPane);
		horizontalPanel.add(rightPanel);
		
		add(topPanel);
		add(horizontalPanel);
		add(bottomPanel);
	}
	
	public Document getDocument() {
		return textPane.getDocument();
	}
	
	public void setText(String text) {
		textPane.setText(text);
		textPane.setCaretPosition(text.length());
	}
	
	public String getText() {
		return textPane.getText();
	}
	
	public void addKeyListener(KeyListener kl) {
		super.addKeyListener(kl);
		textPane.addKeyListener(kl);
	}
	
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		textPane.setEditable(enabled);
	}
	
	public void requestFocus() {
		super.requestFocus();
		textPane.requestFocus();
	}
	
	public void setBackgroundColor(Color c) {
		super.setBackground(c); 
		textPane.setBackground(c); 
		leftPanel.setBackground(c);
		rightPanel.setBackground(c);
	}
	
	public void setFontColor(Color c) {
		textPane.setForeground(c);
	}
	
	public void setCaretColor(Color c) {
		textPane.setCaretColor(c);
	}
	
	public void ensureCaretVisibility() {
		//complete hack to get this in place
		int pos = textPane.getCaretPosition();
		if (textPane.getText().length() == 0) return;  //otherwise moving things around below will cause problems
		textPane.setCaretPosition((pos == 0 ? pos + 1 : pos - 1));
		textPane.setCaretPosition(pos);
	}
	
	public void placeCaretAtEnd() {
		textPane.setCaretPosition(textPane.getText().length());
	}
	
	public void print() {
		textPane.print(textPane.getGraphics());
	}
}
